package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.UsuarioSubprogramaDAO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;

import java.io.IOException;

import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AllocationPermissionReportAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    HttpSession session = request.getSession();
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    
    List lUsuarioSelect = new LinkedList();
    List lSubprogSelect = new LinkedList();
    List Errores = new LinkedList();
    
    //Proceso para la auditoria
    UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
    String terminalAddress = request.getRemoteAddr();
    String strTerminal = ("WEB="+terminalAddress);
    Calendar c = Calendar.getInstance(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
    //Fin del proceso
    
    String strTamanoSubprog = (String)session.getAttribute("TamanoSubpro");
    int iTamanoSubprog = Integer.parseInt(strTamanoSubprog.trim());
    lUsuarioSelect = (List)session.getAttribute("ListadeUsuarios");
    
    for(int i=0;i<lUsuarioSelect.size();i++)
    {
      for(int j=0;j<iTamanoSubprog;j++)
      {
        String strNombre = "subprograma"+j;
        String strCodeSubprog = request.getParameter(strNombre);
        if((Object)strCodeSubprog != null)
        {
          try
          {
            //Proceso para identificar si el usuario ya tiene permisos sobre el beneficio
            UsuarioSubprogramaDAO objUsuSub = new UsuarioSubprogramaDAO(cf);
            List lVerificar = new LinkedList();
            lVerificar = objUsuSub.VerificarPermisosUsuarios(((String)lUsuarioSelect.get(i)),strCodeSubprog);
            cf.closeConnection(objUsuSub.getConn());
            //Fin del proceso
            if(lVerificar.size() > 0 )
            {
              Errores.addAll(lVerificar);
            }else
            {
              String strArr[] = {strCodeSubprog,((String)lUsuarioSelect.get(i)),
                                 user.getName(), dateFormat.format(c.getTime()),strTerminal,
                                 user.getName(), dateFormat.format(c.getTime()),strTerminal};
              UsuarioSubprogramaDAO objUsuSubprog = new UsuarioSubprogramaDAO(cf);
              int iInsertCorrecto = objUsuSubprog.InsertPermisosReportes(strArr);
              cf.closeConnection(objUsuSubprog.getConn());
            }
           
          }catch(SQLException e)
          {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
          }
        }
      }
    }
    if(Errores.size() > 0)
    {
      request.setAttribute("msgerror","Los siguientes usuarios ya tienen permisos sobre los beneficios");  
      request.setAttribute("lErrores",Errores);
    }else
      request.setAttribute("msgc","Los permisos se otorgaron correctamente");
    return mapping.findForward("success");
  }
}